package org.msfj.admin.data.controller;

import org.msfj.admin.model.Dict;
import org.msfj.admin.model.DictIndex;
import org.msfj.admin.vo.DictVo;
import org.msfj.admin.data.service.DictIndexService;
import org.msfj.admin.data.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.msfj.common.base.BaseController;
import org.msfj.common.model.CommonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 系统数据字典明细信息controller
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Api(value = "系统数据字典明细信息接口", tags = "系统数据字典明细信息接口")
@RestController
@RequestMapping("data/dict")
public class DictController extends BaseController{

    @Autowired
    private DictService dictService;
    @Autowired
    private DictIndexService dictIndexService;

    /**
     * 添加系统数据字典明细信息
     *
     * @param dicVo
     * @return CommonResult.ok(DictVo)
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加系统数据字典明细信息",notes = "添加系统数据字典明细信息")
    @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query")
    public CommonResult addDic(DictVo dicVo){
        DictIndex dictIndex = dictIndexService.getById(dicVo.getIndexId());
        if(dictIndex!=null){
            dicVo.setCode(dictIndex.getKey()+"_"+ dicVo.getCode());
        }
        Dict dict = dictService.saveOrEdit(dicVo);
        return CommonResult.ok(dictService.setOtherProperties(dict));
    }

    /**
     * 修改系统数据字典明细信息
     *
     * @param dicVo
     * @return CommonResult.ok(DictVo)
     */
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改系统数据字典明细信息",notes = "修改系统数据字典明细信息")
    @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query")
    public CommonResult editDic(DictVo dicVo){
        Dict dict = dictService.saveOrEdit(dicVo);
        return CommonResult.ok(dictService.setOtherProperties(dict));
    }

    /**
     * 逻辑删除系统数据字典明细信息
     *
     * @param dicVo
     * @return CommonResult.ok(DictVo)
     */
    @PostMapping("/del")
    @ResponseBody
    @ApiOperation(value = "逻辑删除系统数据字典明细信息",notes = "逻辑删除系统数据字典明细信息")
    @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query")
    public CommonResult delDic(DictVo dicVo){
        Dict dict = dictService.delById(dicVo);
        return CommonResult.ok(dictService.setOtherProperties(dict));
    }

    /**
     * 逻辑删除系统用户信息
     * @param ids
     * @return CommonResult.ok()
     */
    @PostMapping("/dels")
    @ResponseBody
    @ApiOperation(value = "逻辑删除系统数据字典明细信息",notes = "逻辑删除系统数据字典明细信息")
    @ApiImplicitParam(value = "ids[]",name = "ids[]",dataType = "java.util.List",paramType = "query")
    public CommonResult delUsers(@RequestParam(value = "ids[]",required = false) List<String> ids){
        Dict dict = new Dict();
        dictService.delByIds(ids, dict);
        return CommonResult.ok();
    }

    /**
     * 真实删除系统数据字典明细信息
     *
     * @param dicVo
     * @return CommonResult.ok()
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "真实删除系统数据字典明细信息",notes = "真实删除系统数据字典明细信息")
    @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query")
    public CommonResult deleteDic(DictVo dicVo){
        dictService.trueDelById(dicVo);
        return CommonResult.ok();
    }

    /**
     * 根据id查找系统数据字典明细信息
     *
     * @param dicVo
     * @return CommonResult.ok(DictVo)
     */
    @GetMapping("/get")
    @ResponseBody
    @ApiOperation(value = "根据id查找系统数据字典明细信息",notes = "根据id查找系统数据字典明细信息")
    @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query")
    public CommonResult getDic(DictVo dicVo){
        Dict dict = dictService.getById(dicVo.getId());
        return CommonResult.ok(dictService.setOtherProperties(dict));
    }

    /**
     * 根据翻页参数、搜索参数查找系统数据字典明细信息
     *
     * @param dicVo
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return CommonResult.ok(PageInfo<DictVo>)
     */
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "根据翻页参数、搜索参数查找系统数据字典明细信息",notes = "根据翻页参数、搜索参数查找系统数据字典明细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query"),
        @ApiImplicitParam(name = "page",value = "page",dataType = "Integer",paramType = "path"),
        @ApiImplicitParam(name = "rows",value = "rows",dataType = "Integer",paramType = "path")
    })
    public CommonResult findDicList(DictVo dicVo, Integer page, Integer rows){
        PageInfo<DictVo> dicVoPageInfo = dictService.getByDic(dicVo,page,rows);
        return CommonResult.ok(dicVoPageInfo);
    }

    /**
     * 根据搜索参数查找所有系统数据字典明细信息
     *
     * @param dicVo
     * @return CommonResult.ok(List<DictVo>)
     */
    @GetMapping("/getAll")
    @ResponseBody
    @ApiOperation(value = "根据搜索参数查找所有系统数据字典明细信息",notes = "根据搜索参数查找所有系统数据字典明细信息")
    @ApiImplicitParam(value = "dicVo",name = "dicVo",dataType = "org.msfj.admin.vo.DictVo",paramType = "query")
    public CommonResult getAllDic(DictVo dicVo){
        List<DictVo> dicVoList = dictService.getAllDic(dicVo);
        return CommonResult.ok(dicVoList);
    }
}
